package com.comac.print.component.app.command.qryExe;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.print.component.app.domain.gateway.enums.SgStatusEnum;
import com.comac.print.component.app.dto.SystemDTO;
import com.comac.print.component.app.gatewayimpl.database.mapper.SystemMapper;
import com.comac.print.component.app.page.PageResponse;
import com.comac.print.component.app.qry.SystemListQry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class SystemQryExe {

    @Autowired
    private SystemMapper systemMapper;


    public PageResponse<SystemDTO> qrySystemList(SystemListQry systemListQry) {
        if(systemListQry.getPageNum()==0 || systemListQry.getPageSize()==0){
            return  PageResponse.buildFailure("-1", "分页参数不为空！");
        }
        Page page = new Page(systemListQry.getPageNum(), systemListQry.getPageSize());
        log.info("---MasterPlateUploadOssQryExe.qryMasterPlateList---查询模板信息，入参：{}", JSON.toJSONString(systemListQry));
        Page<SystemDTO> systemPage = systemMapper.qrySystemList(page,systemListQry);
        log.info("---MasterPlateUploadOssQryExe.qryMasterPlateList---查询模板信息，出参：{}", JSON.toJSONString(systemPage));
        List<SystemDTO> systemDTOList = systemPage.getRecords();
        Page<SystemDTO> pageData = new Page();
        List<SystemDTO> result = putBasicInformation(systemDTOList);
        pageData.setRecords(result);
        pageData.setCurrent(systemPage.getCurrent());
        pageData.setTotal(systemPage.getTotal());
        pageData.setSize(systemPage.getSize());
        return PageResponse.successPage(pageData);
    }

    private List<SystemDTO> putBasicInformation(List<SystemDTO> systemDTODTOList) {
        //设置FAI
        List<SystemDTO> result = new ArrayList<>(systemDTODTOList.size());
        for (SystemDTO systemDTO : systemDTODTOList) {
            SystemDTO systemToDTO = new SystemDTO();
            BeanUtils.copyProperties(systemDTO, systemToDTO);
            //枚举状态的转换
            SgStatusEnum sgStatusEnumByCode = SgStatusEnum.getSgStatusEnumByCode(systemDTO.getStatus());
            systemToDTO.setStatus(sgStatusEnumByCode.getDescription());
            result.add(systemToDTO);
        }
        return result;
    }

}
